<?php
Page::$need_view = false;
ini_set('date.timezone', 'Asia/Shanghai');
error_reporting(0);

file_put_contents(DOXCX_FRAME_PATH.'/log/info/'.date('Y_m_d',time()).'.log', date('H:i:s',time()).'退款回调数据:'.json_encode($_POST),FILE_APPEND);

//获取操作订单表
$bdRefund = new Model('bd_refund');
$bdRefund->find(array('tradeNo'=>$_POST['orderId']));
//获取订单
$m = new Model($bdRefund->tables);
$m->find(['tradeNo' => $_POST['orderId']]);
//如果是平台的订单
if($m->id){
	//商户id
	$wid = $m->wid;
	//支付参数
	$payParams = db_query("SELECT * FROM bd_vshop_weixin_pay WHERE wid=?", [$wid])[0];
	//将平台公钥转换成正确格式的公钥
	$rsaPublicKeyStr = convertRSAKeyStr2Pem($payParams['publicKey']);
	//获取签名
	$_POST['sign']=$_POST['rsaSign'];
	unset($_POST['rsaSign']);
	//校验签名
	$checkSignRes = checkSignWithRsa($_POST, $rsaPublicKeyStr);
	if(!$checkSignRes){
		file_put_contents(DOXCX_FRAME_PATH.'/log/info/'.date('Y_m_d',time()).'.log', date('H:i:s',time()).'退款回调:签名错误',FILE_APPEND);
		echo json_encode(array("errno"=>1,"msg"=>"签名错误","data"=>array()));
		exit();
	}
	if($_POST['refundStatus']==1){
		 //标记为已退款
        $m->is_refund_money = 2;
        $m->save();
		file_put_contents(DOXCX_FRAME_PATH.'/log/info/'.date('Y_m_d',time()).'.log', date('H:i:s',time()).'退款回调:退款成功',FILE_APPEND);
		echo json_encode(array("errno"=>0,"msg"=>"success","data"=>array()));
	}else{
		file_put_contents(DOXCX_FRAME_PATH.'/log/info/'.date('Y_m_d',time()).'.log', date('H:i:s',time()).'退款回调:退款失败',FILE_APPEND);
		echo json_encode(array("errno"=>1,"msg"=>"退款失败","data"=>array()));
	}
	
}else{
	file_put_contents(DOXCX_FRAME_PATH.'/log/info/'.date('Y_m_d',time()).'.log', date('H:i:s',time()).'退款回调:没有找到订单',FILE_APPEND);
	//没有找到此订单
	echo json_encode(array("errno"=>1,"msg"=>"没有找到此订单","data"=>array()));
}







 